import { defineConfig,loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import viteCompression from 'vite-plugin-compression'
import { resolve } from 'path'
import myPlugin from './zip'
var version = ''; // 版本号
(function () {
  var d = new Date();
  var yy = d.getFullYear().toString().slice(2);
  var MM = d.getMonth() + 1 >= 10 ? (d.getMonth() + 1) : '0' + (d.getMonth() + 1);
  var DD = d.getDate() >= 10 ? d.getDate() : '0' + d.getDate();
  var h = d.getHours() >= 10 ? d.getHours() : '0' + d.getHours();
  var mm = d.getMinutes() >= 10 ? d.getMinutes() : '0' + d.getMinutes();
  version = yy + MM + DD + h + mm;
})();
export default defineConfig(({ mode })=>{
  const viteEnv = loadEnv(mode, './')
  return {
    base: viteEnv.VITE_BASE,
    server: {
      host: '0.0.0.0',
      port: '80',
      open: true,
      // 端口占用直接退出
      strictPort: true,
      // 本地服务 CORS 是否开启
      cors: true,
      proxy: {
        [viteEnv.VITE_BASE_URL]: {
          target: viteEnv.VITE_BASE_SERVER_URL,
          // 允许跨域
          changeOrigin: true,
          rewrite: path => path.replace(viteEnv.VITE_BASE_URL, '/')
        }
      }
    },
    build: {
      outDir: 'tradingView_'+version,
      assetsDir: 'static/assets',
      // sourcemap: true,
      // 规定触发警告的 chunk 大小，消除打包大小超过500kb警告
      chunkSizeWarningLimit: 2000,
      // 静态资源打包到dist下的不同目录
      rollupOptions: {
        // input:{
        //   main: resolve(__dirname, 'public/index.html'),
        // },
        output: {
          chunkFileNames: 'static/js/[name]-[hash].js',
          entryFileNames: 'static/js/[name]-[hash].js',
          assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
        }
      }
    },
    resolve: {
      alias: {
        // '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    css: {
      preprocessorOptions: {
         less: {
           modifyVars: {
            //  'arcoblue-6': '#1890FF',
          },
          // additionalData: `@import "@/assets/css/arco-palette.less";`,
          javascriptEnabled: true,
         }
       }
      },
      plugins: [vue(),myPlugin('tradingView_'+version),viteCompression({
        threshold: 1024 * 500, // 对大于500kb的文件进行压缩
        algorithm: 'gzip', // 采用的压缩算法，默认是 gzip
        ext: '.gz', // 生成的压缩包后缀
      })],
 
  }
})
